﻿<cfsilent>
	<cfscript>

		sessionAdvice = getProperty("serviceFactory").getBean("sessionAdvice");
		
		fileObj = getProperty("serviceFactory").getBean("tagFileHelper");
		contentObj = getProperty("serviceFactory").getBean("tagContentHelper");

		target = getProperty("cdnAssetsPath") & "/profile/default_s.jpg";
		
		if ( sessionAdvice.isUserAuthorized() ) {
			
			userId = sessionAdvice.getAutherUserID();
			userGroups = sessionAdvice.getAuthorizedUserGroups();
			
			/*
				P001	教师
				P002	学生
			*/
			if ( listFind( userGroups, "P001", "," ) ) {
				userType = "Teacher";
			}
			if ( listFind( userGroups, "P002", "," ) ) {
				userType = "Student";
			}

			binaryStream = cacheGet( "user_portrait_" & userId, "matrixPicture" );
			
			if ( isNull(binaryStream) ) {
				
				if ( userType eq "Teacher" ) {
					/* 教师 */
					targetNew = getProperty("cdnAssetsPath") & "/teacher/portrait/" & userId & ".jpg";
					if ( fileExists(targetNew) ) {
						target = targetNew;
					}
				}
			
				if ( userType eq "Student" ) {
					/* 学生 */
					targetNew = getProperty("cdnAssetsPath") & "/student/portrait/" & left( userId, 4 ) & "/" & userId & ".jpg";
					if ( fileExists(targetNew) ) {
						target = targetNew;
					}
				}
				
				
				binaryStream = fileObj.readBinary( target );

				/* 将图片对象放置到系统缓存 */
				cachePut( "user_portrait_" & userId, binaryStream, createTimeSpan(2, 0, 0, 0), createTimeSpan(2, 0, 0, 0), "matrixPicture" );
				
			}
			
			
		}
		else {
			
			binaryStream = cacheGet( "user_portrait_default", "matrixPicture" );
			
			if ( isNull(binaryStream) ) {
				
				binaryStream = fileObj.readBinary( target );

				/* 将默认文件对象放置到系统缓存 */
				cachePut( "user_portrait_default", binaryStream, createTimeSpan(2, 0, 0, 0), createTimeSpan(2, 0, 0, 0), "matrixPicture" );
				
			}
			
		}
		
		contentObj.contentWriteThrough( "image/jpeg", binaryStream, true );
		
	</cfscript>
</cfsilent>